package cc.magicjson.easy.batch.application.controller;

import cc.magicjson.easy.batch.application.domain.User;
import cc.magicjson.easy.batch.application.service.UserBatchService;
import cc.magicjson.easy.batch.model.BatchResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/users/easy-batch")
@Slf4j
@RequiredArgsConstructor
public class UserController {
    private final UserBatchService userService;

    @PostMapping("/selector")
    public BatchResult<User> batchInsertUsers(@RequestParam String operation) {
        List<User> users = new ArrayList<>();
        for (int i = 0; i < 30000; i++){
            User user = new User();
            user.setName("User" + i);
            user.setEmail("user" + i + "@example.com");
            users.add(user);
            if("update".toLowerCase().equals(operation)){
                User updateUser = new User();
                updateUser.setId((long) i);
                updateUser.setName("User1" + i);
                updateUser.setEmail("user1" + i + "@example.com");
                users.add(updateUser);
            }
        }
        return switch(operation.toLowerCase()){
            case "insert" -> userService.batchInsertUsers(users);
            case "update" -> userService.batchUpdateUsers(users);
            default -> throw new IllegalStateException("Unexpected value: " + operation.toLowerCase());
        };
    }
}
